<?php
JunctionFileCabinet::using("Junction_Core_Mapping");
JunctionFileCabinet::using("Junction_Core_Property");
JunctionFileCabinet::using("Junction_Mapping_PrimaryKey");
JunctionFileCabinet::using("Junction_Query_Clause_Null");
JunctionFileCabinet::using("Junction_Query_Clause_Where");
JunctionFileCabinet::using("Test_Mock_Client");
JunctionFileCabinet::using("Test_Mock_ClientMKeys");

class Test_Junction_Core_Mapping extends UnitTestCase {
	
	/**
	 * Mapping to test
	 *
	 * @var Junction_Core_Mapping
	 */
	private $_mapping;
	private $_mappingMKeys;

	
	public function testConstruct() {
		
		$key = new Junction_Mapping_PrimaryKey();
		$keyadd = new Junction_Core_Property("Id", "id");
		$key->setProperty($keyadd);
		//$key = new Junction_Core_Property("Id", "id");
		$properties = array();
		$properties[] = new Junction_Core_Property("Email", "email");
		$properties[] = new Junction_Core_Property("Password", "password");
		$properties[] = new Junction_Core_Property("Date", "date_added");
		try {
			$this->_mapping = new Junction_Core_Mapping("Test_Mock_Client", "users", $key, $properties);
			$this->pass();
		} catch (Junction_Core_Exception $e) {
			$this->fail(); 
		}
	}
	
	 public function testConstructMKeys(){
	   $key = new Junction_Mapping_PrimaryKey();
		$keyadd = new Junction_Core_Property("Id", "id");
		$keyadd2 = new Junction_Core_Property("Name","name");
		$keyadd3 = new Junction_Core_Property("Finger","finger_print");
		$key->setProperty($keyadd);
		$key->setProperty($keyadd2);
		$key->setProperty($keyadd3);
		//$key = new Junction_Core_Property("Id", "id");
		$properties = array();
		$properties[] = new Junction_Core_Property("Email", "email");
		$properties[] = new Junction_Core_Property("Password", "password");
		$properties[] = new Junction_Core_Property("Date", "date_added");
		try {
			$this->_mappingMKeys = new Junction_Core_Mapping("Test_Mock_ClientMKeys", "usersMKeys", $key, $properties);
			$this->pass();
		} catch (Junction_Core_Exception $e) {
			$this->fail(); 
		}
	}
	
//	public function testContains() {
//		$this->assertTrue($this->_mapping->contains("Id"));
//		$this->assertTrue($this->_mapping->contains("Date"));
//		$this->assertFalse($this->_mapping->contains("Foobar"));
//	}
	
//	public function testTranslate() {
//		$this->assertTrue($this->_mapping->translate("Id") == "id");
//		$this->assertTrue($this->_mapping->translate("Date") == "date_added");
//		$this->assertTrue($this->_mapping->translate("foobar") == null);
//	}
	
	public function testCreateClient() {
		$data = array("id" => 1, "email" => "foo@bar.com", "password" => "apsw", "date_added" => time());
		$client = $this->_mapping->makeClientFrom($data);
		$this->assertTrue(is_a($client, "Test_Mock_Client"));
		$this->assertTrue($client->getId() == 1);
		$this->assertTrue($client->getEmail() == "foo@bar.com");
		$this->assertTrue($client->getPassword() == "apsw");
	}
	
    public function testCreateClientMKeys() {
		$data = array("id" => 1, "name"=>"Bob", "finger_print" =>"tasty", "email" => "foo@bar.com", "password" => "apsw", "date_added" => time());
    	$client = $this->_mappingMKeys->makeClientFrom($data);
		$this->assertTrue(is_a($client, "Test_Mock_ClientMKeys"));
		$this->assertFalse(is_a($client,"Test_Mock_Client"));
		$this->assertTrue($client->getId() == 1);
		$this->assertTrue($client->getName() == "Bob");
		$this->assertTrue($client->getFinger()=="tasty");
		$this->assertTrue($client->getEmail() == "foo@bar.com");
		$this->assertTrue($client->getPassword() == "apsw");
	}
	
	public function testSave() {
		$client = new Test_Mock_Client();
		$client->setPassword("apsw");
		$client->setEmail("foo@bar.com");
		$client->setDate(time());
		$this->assertTrue($this->_mapping->insert($client));
		$this->assertTrue($client->getId() > 0);
		$client->setEmail("bar@foo.com");
		$this->assertTrue($this->_mapping->update($client));
	}
	
    public function testSaveMKeys() {
    	$client = new Test_Mock_ClientMKeys();
    	$client->setId(21);
    	$client->setName("Bobby");
    	$client->setFinger("spotted");
		$client->setPassword("apsw");
		$client->setEmail("foo@bar.com");
		$client->setDate(time());
		$this->assertTrue($this->_mappingMKeys->insert($client));
		$this->assertTrue($client->getId() > 16);
		$client->setEmail("bar@foo.com");
		$this->assertTrue($this->_mappingMKeys->update($client));
	}
	
//	public function testSaveMkeysBad(){
//		$client = new Test_Mock_ClientMKeys();
//    	$client->setName("Tim");
//    	$client->setFinger("pokadot");
//		$client->setPassword("eek");
//		$client->setEmail("moo@cow.com");
//		$client->setDate(time());
//		$this->assertFalse($this->_mappingMKeys->insert($client));
//	}
	
	public function testFetch() {		
		$clients = $this->_mapping->loadBy(Junction_Query_Clause_Null::construct());
		$results = array();
		foreach ($clients as $client) {
			$results[] = $client;
		}
		$this->assertTrue(count($results) > 0);
		$this->assertTrue($results[0]->getId() > 0);
	}
	
    public function testFetchMKeys() {		
		$clients = $this->_mappingMKeys->loadBy(Junction_Query_Clause_Null::construct());
		$results = array();
		foreach ($clients as $client) {
			$results[] = $client;
		}
		$this->assertTrue(count($results) > 0);
		$this->assertTrue($results[0]->getId() > 0);
	}
	
	public function testDelete() {
		$clause = new Junction_Query_Clause_Where("Email = ?");
		$clause->bind(0, "bar@foo.com");
		$this->assertTrue($this->_mapping->deleteBy($clause) > 0);
	}

	public function testDeleteMKeys() {
		$clause = new Junction_Query_Clause_Where("Name = ?");
		$clause->bind(0, "Bobby");
		$this->assertTrue($this->_mappingMKeys->deleteBy($clause) > 0);
	}
}
?>